package com.jiayou.activity

import android.content.Intent
import android.graphics.BitmapFactory
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.annotation.RequiresApi
import androidx.recyclerview.widget.LinearLayoutManager
import cn.hutool.http.HttpRequest
import cn.hutool.json.JSONUtil
import com.jiayou.R
import com.jiayou.activity.adapter.Moment
import com.jiayou.activity.adapter.MomentAdapter
import com.jiayou.tool.AndroidTool
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.activity_moments.*
import kotlinx.android.synthetic.main.activity_moments.back
import kotlinx.android.synthetic.main.activity_moments.imageView3
import kotlinx.android.synthetic.main.activity_publish_moment.*
import kotlinx.android.synthetic.main.menu_item.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import java.util.*
import kotlin.collections.ArrayList
import kotlin.concurrent.thread

class MomentsActivity : AppCompatActivity(), View.OnClickListener {

    private val properties = Properties()
    lateinit var uri: String


    @RequiresApi(Build.VERSION_CODES.M)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_moments)
        AndroidTool.fullScreen(this, "#F2F2F2")


        properties.load(application.assets.open("im.properties"))
        uri = properties["im.uri"].toString() + ":" + properties["im.port"].toString()



        back.setOnClickListener(this)
        wodetouxiang.setOnClickListener(this)
        imageView3.setOnClickListener(this)

        initInfo()
        initMoments()

    }

    override fun onClick(p0: View) {
        when (p0.id) {
            R.id.back -> {
                finish()
            }
            R.id.wodetouxiang -> {
            }
            R.id.imageView3 -> {
                startActivity(Intent(this, PublishMomentActivity::class.java))
            }
        }
    }

    private fun initMoments() {
        thread {
            val resoult = JSONUtil.parseObj(
                HttpRequest.get("http://${uri}/im/moment/getList")
                    .form(
                        mapOf(
                            "id" to MainActivity.ID
                        )
                    )
                    .header("auth", "73913da7-508b-4646-b8ae-ef6458f41153")
                    .execute()
                    .body()
            )

            if (resoult["code"].toString().toInt() == 200) {
                val list = ArrayList<Moment>()
                val momentList = JSONUtil.parseArray(resoult["data"])
                momentList.forEach {
                    val obj = JSONUtil.parseObj(it)
                    list.add(
                        Moment(
                            id = obj["id"].toString().toInt(),
                            username = obj["username"].toString(),
                            petname = obj["petname"].toString(),
                            img = obj["img"].toString(),
                            description = obj["description"].toString()
                        )
                    )
                }
                runOnUiThread {
                    momentsList.adapter = MomentAdapter(this, list = list)
                    val manager = LinearLayoutManager(this)
                    manager.orientation = LinearLayoutManager.VERTICAL
                    momentsList.layoutManager = manager
                }

            }

        }
    }

    override fun onBackPressed() = finish()

    private fun initInfo() {

        wodewangming.text = MainActivity.petname
        wodeqianming.text = MainActivity.description
        thread {
            val bodyStream = HttpRequest.get("http://$uri/im/userinfo/getImgByName").form("img", MainActivity.img)
                .header("auth", "73913da7-508b-4646-b8ae-ef6458f41153")
                .execute()
                .bodyStream()

            runBlocking {
                withContext(Dispatchers.Main) {
                    wodetouxiang.setImageBitmap(BitmapFactory.decodeStream(bodyStream))
                }
            }
        }
    }
}